package com.woniuxy.java106crm.service.impl;

import com.woniuxy.java106crm.entity.Perms;
import com.woniuxy.java106crm.entity.Role;
import com.woniuxy.java106crm.mapper.RoleMapper;
import com.woniuxy.java106crm.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户角色业务逻辑层
 */
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    //查询全部角色
    @Override
    public List<Role> queryRoles() {
        return roleMapper.queryRoles();
    }
    //添加角色


    @Transactional
    @Override
    public void assignRolePerms(Integer id, ArrayList<Integer> permsId) {
        //删除角色权限关联关系
        roleMapper.deleteRolePerms(id);
        //添加角色的新的权限
        permsId.forEach(p->{
            roleMapper.addRolePerms(id,p);
        });

    }

    @Override
    public Boolean addRole(Role role) {
        return roleMapper.addRole(role);
    }
    //删除角色
    @Transactional
    @Override
    public Boolean deleteRole(Integer id) {
        //删除角色权限关联关系
        roleMapper.deleteRolePerms(id);
        //删除用户角色关联关系表
        roleMapper.deleteUserRole(id);
        //删除角色
        return roleMapper.deleteRole(id);
    }
}
